/*
 * Surface.cpp
 *
 *  Created on: Nov 19, 2012
 *      Author: lucas
 */

#include "Surface.h"
#include "PhysicsPoint.h"
#include "Vector2D.h"
Surface::Surface() {
	this->setHitPoints(MIN_HITPOINTS);
}

Surface::~Surface() {
	std::list<PhysicsPoint *>::iterator i;
	this->points.pop_front();
	this->points.pop_back();
	for (i = this->points.begin() ; i != this->points.end() ; ++i)
		delete (*i);
}

void Surface::checkCollision(PhysicsElement &other) {
	std::list<PhysicsPoint *>::iterator i;
	for (i = this->points.begin() ; i != this->points.end() ; ++i)
		(*i)->checkCollision(other);
}

void Surface::onCollisionResponse(PhysicsElement &other) {

}

void Surface::add(PhysicsPoint &point) {
	// TODO limite la cantidad de puntos a 2 !!!!!!!!!
	if (this->points.size() < 2)
		this->points.push_back(&point);
}

void Surface::remove(PhysicsPoint &point) {
	this->points.remove(&point);
}

void Surface::approximateLine(int totalPoints) {
	// TODO limite la cantidad de puntos a 2 !!!!!!!!!
	if (this->points.size() < 2)
		return;

	Vector2D v1(this->points.front()->getX(), this->points.front()->getY());
	Vector2D v2(this->points.back()->getX(), this->points.back()->getY());
	double distance = (v1 - v2).getMagnitude() / ((double) totalPoints);
	double position = 0;
	PhysicsPoint *back = this->points.back();
	this->points.pop_back();
	for (int i = 0 ; i < totalPoints ; i++) {
		this->points.push_back(new PhysicsPoint(v1.getX() + position, v1.getY()));
		position += distance;
	}
	this->points.push_back(back);
}
